package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.domain.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 会员数据的实现层
 */
@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    /**
     * 通过手机号查询会员
     *
     * @param telephone
     * @return
     */
    @Override
    public Member selectByTelephone(String telephone) {
        return memberDao.selectMemberByPhone(telephone);
    }


    /**
     * 添加会员
     *
     * @param member
     */
    @Override
    public void add(Member member) {
        if (!(StringUtils.isEmpty(member.getPassword()))) {
            String password = MD5Utils.md5(member.getPassword());
            member.setPassword(password);
        }
        memberDao.insertMember(member);
    }


    /**
     * 通过月份查找会员当月注册数量
     *
     * @param months
     * @return
     */
    @Override
    public ArrayList<Integer> selectMemberCountByMonth(ArrayList<String> months) {
        ArrayList<Integer> list = new ArrayList<>();
        for (String month : months) {
            Integer count = memberDao.selectMemberCountByMonth(month);
            list.add(count);
        }
        return list;
    }
}
